<?php

namespace App\Http\Controllers\Api;

use App\Model\OperationLogs;
use App\Model\UserFeedback;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;

class UserFeedbackController extends BaseController
{
    public function __construct(Request $request, UserFeedback $userFeedback)
    {
        parent::__construct($request);
        $this->model = $userFeedback;
    }
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $limit = $request->input('limit',10);
        $page = $request->input('page', 1);
        $uuid = $request->input('uuid', '');
        $nickname = $request->input('nickname', '');
        $content = $request->input('fe_content', '');
        $status = $request->input('fe_status');
    
        $query = $this->model;
        if($content) $query = $query->where('fe_content','like', "%{$content}%");
        if(isset($status)) $query = $query->where('fe_status','=', $status);
        $result = $query->with(['user' => function($query) use ($uuid, $nickname){
            if($uuid) $query->where(['uuid' => $uuid]);
            if($nickname) $query->where('nickname','like',"{$nickname}%");
        }])->paginate($limit,['*'],'page',$page);
        
        $result = collect($result)->map(function ($item,$key){
            if($key === 'data'){
                return collect($item)->filter(function ($back,$key){
                    return $back['user'];
                });
            }
            return $item;
        });

        $result = objToArray($result);
        foreach($result['data'] as &$val) {
            $val['fe_imgs'] = json_decode($val['fe_imgs'], true);
            if(!empty($val['fe_imgs'])) {
                foreach($val['fe_imgs'] as &$v2) {
                    $v2 = build_image($v2);
                }
            }
        }
    
        return $this->toJson(self::SUCCESS, $result);
    }

    /**
     * 更改反馈状态
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function ChangeStatus(Request $request)
    {
        //接收参数
        $param = $request->all();
        if (empty($param['id']))
        {
            return $this->toJson(self::ERROR, [],'缺少id');
        }
        if (empty($param['note']))
        {
            return $this->toJson(self::ERROR, [],'备注不能为空');
        }
        $res = $this->model->ChangeStatus($param);
        if ($res['code'] !== 0) {
            return $this->toJson(self::ERROR,[],$res['msg']);
        }
        return $this->toJson(self::SUCCESS, [],'处理成功');
    }
}
